﻿using DGIS.GscloudService.IService;
using DGIS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace DGIS.GscloudService.Service
{
    public class GSUserService : IGSUserService
    {
        public bool AddGSUser(string userName, string psw)
        {
            DataAccessService.IService.ISqliteService sqliteService = new DataAccessService.Service.SqliteService();

            string delSql = string.Format("delete from GSUser where UserName='{0}'", userName);
            System.Data.SQLite.SQLiteConnection conn = sqliteService.GetConn();
            using (System.Data.SQLite.SQLiteTransaction tran=conn.BeginTransaction())
            {
                if(sqliteService.ExcuteSql(conn, tran, delSql))
                {
                    if(sqliteService.AddItem<GSUser>(conn,tran,new GSUser()
                    {
                        UserName = userName,
                        Psw = Encryption(psw),
                        Type=1
                    }, "id"))
                    {
                        tran.Commit();
                        conn.Close();

                        return true;
                    }
                    else
                    {
                        tran.Rollback();
                        conn.Close();

                        return false;
                    }
                }
                else
                {
                    tran.Rollback();
                    conn.Close();

                    return false;
                }
            }
           
        }        

        public List<GSUser> GetGSUserList()
        {
            DataAccessService.IService.ISqliteService sqliteService = new DataAccessService.Service.SqliteService();
            List<GSUser> items = sqliteService.GetList<GSUser>("1=1");

            foreach (GSUser item in items)
            {
                item.Psw = Decode(item.Psw);
            }

            return items;
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string Encryption(string str)
        {
            string psw;
            string key = "tracymac";
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
                des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                des.IV = ASCIIEncoding.ASCII.GetBytes(key);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                psw = Convert.ToBase64String(ms.ToArray());
                ms.Close();                
            }
            return psw;
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="psw"></param>
        /// <returns></returns>
        private string Decode(string psw)
        {
            string str;
            string key = "tracymac";
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Convert.FromBase64String(psw);
                des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                des.IV = ASCIIEncoding.ASCII.GetBytes(key);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();                
            }
            return str;
        }
    }
}
